package com.example.lin.zyfypt_327ljd.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lin.zyfypt_327ljd.R;
import com.example.lin.zyfypt_327ljd.adapter.MyAdapter3;
import com.example.lin.zyfypt_327ljd.bean.VideoBean;
import com.example.lin.zyfypt_327ljd.iface.VideoListener;
import com.example.lin.zyfypt_327ljd.model.VideoModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment32 extends Fragment {
    //private TextView tvinfo;
    private View view=null;
    private Context context;

    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;//显示布局效果
    private MyAdapter3 myAdapter3;//适配器
    private List<VideoBean> list3=null;//数据源

    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置

    private SharedPreferences sp;
    private String sessionid="";


    private VideoListener videoListener=new VideoListener() {
        @Override
        public void onResponse(List<VideoBean> beanlist) {
            //list3=beanlist;//得到网络数据
            if(page==1)
            {
                list3=beanlist;
            }
            else {
                list3.removeAll(beanlist);
                list3.addAll(beanlist);
            }
            myAdapter3.setList(list3);//传给adapter
            myAdapter3.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg2) {
            Toast.makeText(context,msg2,Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        view=inflater.inflate(R.layout.fragment31,container,false);
//        tvinfo=(TextView)view.findViewById(R.id.textView8);
//        tvinfo.setText("专题");
        Toast.makeText(context, "加载Fragment专题", Toast.LENGTH_SHORT).show();

        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();

        initRecyclerView();

        VideoModel videoModel=new VideoModel();
        videoModel.getSpecialResultList("video",
                page,
               sessionid,
                videoListener);

        return view;
    }
    private void initRecyclerView() {
        //获取RecyclerView,设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
        //创建默认线性布局
        layoutManager=new LinearLayoutManager(context);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adapter
        //list=getData();

        myAdapter3=new MyAdapter3(context);
        myAdapter3.setList(list3);
        //绑定RecyclerView和adapter
        recyclerView.setAdapter(myAdapter3);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list3.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    VideoModel videoModel = new VideoModel();
                    videoModel.getResultList("video", page, sessionid,videoListener);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });

    }
    private void readSP() {
        sessionid=sp.getString("sessionid",null);
    }
}

